
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for add_photo_album_comments.
	''' </summary>
	Public Partial Class add_photo_album_comments
		Inherits BaseWebPage
		Private imgID As Integer = -1
		Protected htPageConstants As New Hashtable()

		Private usrID As Integer = -1
		Private grpID As Integer = -1
		Private isGroupMode As Boolean = False

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.ADD_PHOTO_ALBUM_COMMENTS_PAGE;
			If LoggedUser = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			' init page from XML
			Me.initPageTitles(Constants.ADD_PHOTO_ALBUM_COMMENTS, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			Try
				Dim usr As User = Nothing
				Dim grp As Group = Nothing

				imgID = Convert.ToInt32(Request.Params("img"))
				' init user
				If Request.Params("usr") = Nothing Then
					usr = LoggedUser
				Else
					usrID = Convert.ToInt32(Request.Params("usr"))
					usr = Users.GetUserByID(usrID)
				End If

				' init group
				If Request.Params(Constants.GROUP_PARAM) <> Nothing AndAlso Request.Params(Constants.GROUP_PARAM) <> "" Then
					isGroupMode = True
					grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
					grp = Groups.GetGroupByID(grpID)
					If grp = Nothing Then
						Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
					End If
					If Not grp.Members.Contains(usr.ID) Then
						Throw New Exception(Constants.ERROR_MESSAGE_ONLY_MEMBERS_ACCESS)
					End If
				End If

				' init photo
				If isGroupMode Then
					Dim pic As GroupPicture = grp.GetPicture(imgID)
					imgPhoto.ImageUrl = Pictures.GetPictureSrcFullURL(pic, False, "../")
					hlBackToGroup.NavigateUrl = "../" + Constants.GROUP_WELCOME + "?" + Constants.GROUP_PARAM + "=" + grpID
					hlBackToGroup.Text = (Me.GetPageConstText(htPageConstants, "CONST_GroupBackLink_TEXT")).Replace("[PARAM]", grp.Name)

					' init description
					If pic.Desc <> "" Then
						lblDescription.Text = (Me.GetPageConstText(htPageConstants, "CONST_Description_TEXT")).Replace("[PARAM]", pic.Desc)
					Else
						lblDescription.Visible = False
					End If

					'btnPost.Attributes["onclick"] = "javascript:return confirm('" + Constants.GONFIRM_MESSAGE_ADD_GROUP_PHOTO_ALBUM_COMMENTS + "');";

					trUserShortCut.Visible = False
				Else
					If Not usr.DirectFriends.Contains(LoggedUser.ID) Then
						Response.Redirect(Constants.PHOTO_ALBUM_COMMENTS_CONFIRMATION_PAGE + "?img=" + imgID + "&usr=" + usrID + "&conf=no")
					End If

					Dim pic As UserPicture = usr.GetPicture(imgID)
					imgPhoto.ImageUrl = Pictures.GetPictureSrcFullURL(pic, False, "../")

					' init description
					If pic.Desc <> "" Then
						lblDescription.Text = (Me.GetPageConstText(htPageConstants, "CONST_Description_TEXT")).Replace("[PARAM]", pic.Desc)
					Else
						lblDescription.Visible = False
					End If

					'btnPost.Attributes["onclick"] = "javascript:return confirm('" + Constants.GONFIRM_MESSAGE_ADD_USER_PHOTO_ALBUM_COMMENTS + "');";

					If usr <> Nothing AndAlso usr.ID <> LoggedUser.ID Then
						ucMemberShortcut.UserID = usr.ID
					Else
						trUserShortCut.Visible = False
					End If
				End If

				'btnCancel.Attributes["onclick"] = "javascript: history.back(); return false;";

				txtComments.Attributes.Add("onKeyDown", Constants.ON_KEY_DOWN_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_PICTURE_COMMENTS_MAX_CHARS.ToString()))
				txtComments.Attributes.Add("onKeyUp", Constants.ON_KEY_UP_SCRIPT.Replace(Constants.TEXT_AREA_MAX_CHARS_TAG, Constants.TEXT_AREA_PICTURE_COMMENTS_MAX_CHARS.ToString()))
			Catch generatedExceptionName As Exception
				Throw
			End Try
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()

		End Sub
#End Region

		Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If Page.IsValid Then
				Dim loggedUsr As User = LoggedUser
				Dim usr As User = Users.GetUserByID(usrID)
				Dim DirectFriends As UsersFriends = usr.DirectFriends

				Dim comment As String = removeScriptTags(txtComments.Text.Trim())
				comment = removeScriptTags(comment.Substring(0, Math.Min(comment.Length, Constants.TEXT_AREA_USER_COMMENTS_MAX_CHARS)))

				lblComment.Text = comment
				trCommentPost.Visible = False
				trConfrimation.Visible = True
				lblTitle.Text = Me.GetPageConstText(htPageConstants, "CONST_ConfirmTitle_TEXT")
			End If
		End Sub

		Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			If isGroupMode Then
				Response.Redirect(Constants.VIEW_GROUP_PHOTOS + "?img=" + imgID + "&" + Constants.GROUP_PARAM + "=" + grpID)
			Else
				Response.Redirect(Constants.VIEW_MEMBER_PHOTOS_PAGE + "?img=" + imgID + "&id=" + usrID)
			End If
		End Sub

		Protected Sub btnPost_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Dim loggedUsr As User = LoggedUser
			Dim usr As User = Users.GetUserByID(usrID)
			Dim DirectFriends As UsersFriends = usr.DirectFriends

			Dim temp As String = removeScriptTags(lblComment.Text.Trim())
			Dim comStatus As PictureComment.Status = PictureComment.Status.pcsNew

			If isGroupMode Then
				Dim grp As Group = Groups.GetGroupByID(grpID)
				Dim pic As GroupPicture = grp.GetPicture(imgID)
				If usr.ID = grp.Uid OrElse grp.AutoApproveComments Then
					comStatus = PictureComment.Status.pcsAccepted
				End If

				pic.AddComment(LoggedUser.ID, temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_AREA_PICTURE_COMMENTS_MAX_CHARS)), comStatus)
				Response.Redirect(Constants.PHOTO_ALBUM_COMMENTS_CONFIRMATION_PAGE + "?img=" + imgID + "&usr=" + usrID + "&conf=yes" + "&" + Constants.GROUP_PARAM + "=" + grp.ID)
			Else
				If DirectFriends.Contains(loggedUsr.ID) Then
					Dim pic As UserPicture = usr.GetPicture(imgID)
					If usr.ApproveComments Then
						comStatus = PictureComment.Status.pcsNewAccepted
					End If
					'comStatus = PictureComment.Status.pcsAccepted;

					pic.AddComment(LoggedUser.ID, temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_AREA_PICTURE_COMMENTS_MAX_CHARS)), comStatus)
					Response.Redirect(Constants.PHOTO_ALBUM_COMMENTS_CONFIRMATION_PAGE + "?img=" + imgID + "&usr=" + usrID + "&conf=yes")
				Else
					Response.Redirect(Constants.PHOTO_ALBUM_COMMENTS_CONFIRMATION_PAGE + "?img=" + imgID + "&usr=" + usrID + "&conf=no")
				End If
			End If
		End Sub

		Protected Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			trCommentPost.Visible = True
			trConfrimation.Visible = False
		End Sub

		====================
        ' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
        Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
            Get
                Return Migrated_Form1
            End Get
        End Property
        'set { Migrated_Form1 = value; }
    End Class
End Namespace






